package com.occludens.util;

import java.util.Locale;
import java.util.ResourceBundle;

public class i18nNow {
	
	public static String getString(String bundle, String key, String language) throws Exception {
		try {
			String returnString = "";
						
			if (!(bundle!=null && !bundle.isEmpty())) {
				throw new Exception(ResourceBundle.getBundle("ErrorMessageStringResource").getString("Error.i18nNow.Bundle.NotDefined"));
			}
			
			if (!(key!=null && !key.isEmpty())) {
				throw new Exception(ResourceBundle.getBundle("ErrorMessageStringResource").getString("Error.i18nNow.Key.NotDefined"));
			}
			
			Locale locale = null;
			if (language!=null && !language.isEmpty()) {
				locale = new Locale(language);
			} else {
				locale = Locale.US;
			}
					
			ResourceBundle resourceBundle = ResourceBundle.getBundle(bundle, locale);
			if (resourceBundle!=null) {
				if (resourceBundle.containsKey(key)) {
					returnString = resourceBundle.getString(key);
				} else {
					throw new Exception(ResourceBundle.getBundle("ErrorMessageStringResource").getString("Error.i18nNow.Key.NotExist"));
				}
			} else {
				throw new Exception(ResourceBundle.getBundle("ErrorMessageStringResource").getString("Error.i18nNow.Bundle.NotExist"));
			}
					
			return returnString;
		} catch (Exception e) {
			throw e;
		}
	}

}
